/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#ifndef SX_STACK_H
#define SX_STACK_H

/*
 * The implementation of the Stack class depends on the implementaiton of a
 * basic list. Since the template parameters of this class prevent the use of
 * an opaque pointer, this implementation detail cannot be hidden.
 */
#include <sxLinkedList.h>

namespace Sx {
namespace Util {

/*
 * The Stack class provides a basic implementation of a standard stack data
 * structure. The default standard library allocator is provided as the default
 * allocator.
 */
template <typename T, typename allocator = std::allocator<T> >
class Stack {
public:
	Stack();
	Stack(Stack<T, allocator>& stack);
	~Stack();

	/*
	 * Provides a reference to the object on the top of this Stack without
	 * removing it.
	 */
	const T& peek() const;

	/*
	 * Removes the object on the top of this Stack and returns it.
	 */
	T pop();

	/*
	 * This function pushes the provided data onto the top of this Stack.
	 */
	void push(const T& data);

	/*
	 * Clear all of the items out of the ArrayList.
	 */
	void clear();
	void empty();

	/*
	 * If this Stack does not contain any elements this function will
	 * return true; otherwise it will return false.
	 */
	bool isEmpty() const;

	/*
	 * Returns the number of elements in contained in this Stack.
	 */
	unsigned int size() const;

	/*
	 * Assignment operator for copying a Stack.
	 */
	Stack<T, allocator>& operator = (Stack<T, allocator>& s);

private:
	typedef LinkedList<T, allocator> StackType;
	StackType imp;
};

template <typename T, typename allocator>
Sx::Util::Stack<T, allocator>::Stack() {
	this->imp = StackType();
}

template <typename T, typename allocator>
Sx::Util::Stack<T, allocator>::Stack(Stack<T, allocator>& stack) {
	this->imp = stack.imp;
}

template <typename T, typename allocator>
Sx::Util::Stack<T, allocator>::~Stack() {}

template <typename T, typename allocator>
const T& Sx::Util::Stack<T, allocator>::peek() const {
	return this->imp.first();
}

template <typename T, typename allocator>
T Sx::Util::Stack<T, allocator>::pop() {
	T data = this->imp.getFirst();
	this->imp.removeFirst();
	return data;
}

template <typename T, typename allocator>
void Sx::Util::Stack<T, allocator>::push(const T& data) {
	this->imp.addFirst(data);
}

template <typename T, typename allocator>
void Sx::Util::Stack<T, allocator>::clear() {
	this->imp.clear();
}

template <typename T, typename allocator>
void Sx::Util::Stack<T, allocator>::empty() {
	this->imp.empty();
}

template <typename T, typename allocator>
bool Sx::Util::Stack<T, allocator>::isEmpty() const {
	if ( this->imp.size() == 0 ) return true;
	return false;
}

template <typename T, typename allocator>
unsigned int Sx::Util::Stack<T, allocator>::size() const {
	return this->imp.size();
}

template <typename T, typename allocator>
Sx::Util::Stack<T, allocator>& Sx::Util::Stack<T, allocator>::operator = (Stack<T, allocator>& s) {
	if ( this == &s ) return *this;
	this->imp = s.imp;
}

}

}

#endif
